package com.yunquan.ohana.base.dialog

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.appcompat.app.AppCompatDialog
import androidx.viewbinding.ViewBinding
import com.yunquan.ohana.R

abstract class BaseDialog<VB : ViewBinding>(context: Context) :
    AppCompatDialog(context, R.style.DialogTheme), View.OnClickListener {
    protected lateinit var binding: VB

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = createViewBinding()
        setContentView(binding.root)
        // 设置外部取消
        setCancelable(cancelable())
        setCanceledOnTouchOutside(cancelable())
        // 设置进出动画
        window?.setWindowAnimations(setAnimRes())
        // 设置位置
        window?.setGravity(setGravity())
        initData()
        initView()
    }

    protected abstract fun createViewBinding(): VB

    protected open fun cancelable(): Boolean = true

    protected open fun setAnimRes(): Int = android.R.style.Animation_Dialog

    protected open fun setGravity(): Int = Gravity.CENTER

    protected abstract fun initData()

    protected abstract fun initView()

    override fun show() {
        if (isShowing) return
        super.show()
    }
}